﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AtletIS.Shared.Data;
using AtletIS.Shared.Helpers;

namespace AtletIS.Shared.Forms.Admin
{
    public partial class frmAdminClubs : Form
    {
        private aDbDataSet.ClubsRow drActiveClub;
        private bool bNew;
        
        // Constructor
        public frmAdminClubs()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.AtletIS64;
        }
        
        // Load
        private void frmAdminClubs_Load(object sender, EventArgs e)
        {
            tsbCancel.Enabled = false;
            tsbSave.Enabled = false;
            this.clubsTableAdapter.Fill(this.aDbDataSet.Clubs);
        }

        // Selection changed
        private void clubsDataGridView_SelectionChanged(object sender, EventArgs e)
        {
            if (clubsDataGridView.CurrentRow == null)
                return;

            try
            {
                DataRowView drv = (DataRowView)clubsDataGridView.CurrentRow.DataBoundItem;
                chkActive.Checked = Convert.ToBoolean(drv["Active"]);
                txtID.Text = drv["ID"].ToString();
                txtName.Text = drv["Name"].ToString();
                txtCity.Text = drv["City"].ToString();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Kluby", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // New
        private void tsbNew_Click(object sender, EventArgs e)
        {
            try
            {
                bNew = true;
                tsbNew.Enabled = false;
                tsbEdit.Enabled = false;
                tsbCancel.Enabled = true;
                tsbSave.Enabled = true;

                chkActive.Checked = true;
                txtID.Text = string.Empty;
                txtName.Text = string.Empty;
                txtCity.Text = string.Empty;

                this.drActiveClub = aDbDataSet.Clubs.NewClubsRow();

                this.drActiveClub["Active"] = true;
                this.drActiveClub["Sync"] = false;

                gbList.Enabled = false;
                gbDetails.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Kluby - pridať", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Edit
        private void tsbEdit_Click(object sender, EventArgs e)
        {
            try
            {
                bNew = false;
                tsbNew.Enabled = false;
                tsbEdit.Enabled = false;
                tsbCancel.Enabled = true;
                tsbSave.Enabled = true;

                DataRowView drv = (DataRowView)clubsDataGridView.CurrentRow.DataBoundItem;
                this.drActiveClub = (aDbDataSet.ClubsRow)drv.Row;

                gbList.Enabled = false;
                gbDetails.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Kluby - editovať", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Cancel
        private void tsbCancel_Click(object sender, EventArgs e)
        {
            tsbNew.Enabled = true;
            tsbEdit.Enabled = true;
            tsbCancel.Enabled = false;
            tsbSave.Enabled = false;

            gbList.Enabled = true;
            gbDetails.Enabled = false;

            clubsDataGridView_SelectionChanged(null, null);
        }

        // Save
        private void tsbSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtID.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Zadajte ID klubu!", "Kluby - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                
                if (txtName.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Zadajte názov!", "Kluby - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                if (txtCity.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Zadajte mesto!", "Kluby - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                this.drActiveClub["Active"] = chkActive.Checked;
                this.drActiveClub["ID"] = txtID.Text;
                this.drActiveClub["Name"] = txtName.Text;
                this.drActiveClub["City"] = txtCity.Text;
                this.drActiveClub["Modified"] = DateTime.Now;

                if (bNew)
                {
                    aDbDataSet.Clubs.Rows.Add(this.drActiveClub);
                    this.clubsTableAdapter.Update(aDbDataSet.Clubs);
                }
                else
                {
                    this.clubsTableAdapter.Update(this.drActiveClub);
                }

                this.clubsTableAdapter.Fill(this.aDbDataSet.Clubs);

                tsbNew.Enabled = true;
                tsbEdit.Enabled = true;
                tsbCancel.Enabled = false;
                tsbSave.Enabled = false;

                gbDetails.Enabled = false;
                gbList.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Kluby - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Close
        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsbImport_Click(object sender, EventArgs e)
        {
            try
            {
                string fileName = string.Empty;

                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "CSV súbory (*.csv)|*.csv";
                dlg.Title = "Vyberte CSV súbor na import";

                if (dlg.ShowDialog() == DialogResult.OK)
                    fileName = dlg.FileName;

                if (string.IsNullOrEmpty(fileName))
                    return; // user didn't select a file to open

                Cursor.Current = Cursors.WaitCursor;

                // Import from file
                RegistrationImportHelper.ImportClubs(fileName);

                // Reload
                this.clubsTableAdapter.Fill(this.aDbDataSet.Clubs);

                MessageBox.Show("Import klubov prebehol úspešne.", "Import klubov", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ShowErrorInfo(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        








    }
}
